﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Classical Meanfilter using a default convolution matrix
    /// </summary>
    public class MeanFilter : BaseImageFilter
    {
        protected int m_nWidth = 10;
        CSharpMask matrix = new CSharpMask();

        /// <summary>
        /// Initializes a new instance of the <see cref="MeanFilter"/> class.
        /// </summary>
        public MeanFilter()
            : this(10)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MeanFilter"/> class.
        /// </summary>
        /// <param name="_nWidth">Width of the _n.</param>
        public MeanFilter(int _nWidth)
        {
            m_nWidth = _nWidth;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MeanFilter"/> class.
        /// </summary>
        /// <param name="_meanFilter">The _mean filter.</param>
        internal MeanFilter(MeanFilter _meanFilter)
        {
        }

        /// <summary>
        /// Gets or sets the width.
        /// </summary>
        /// <value>The width.</value>
        public int Width
        {
            get { return m_nWidth; }
            set { m_nWidth = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            matrix.SetAll(1);
            matrix.Pixel = m_nWidth;
            matrix.Factor = 9;
            matrix.Offset = 0;

            new SimpleConvolution(matrix).Execute(_bitmap);
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new MeanFilter(this);
        }
    }
}
